package cn.kgc.vue.controller;

import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.form.TemplateLsitForm;
import cn.kgc.vue.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/***
 *@title TemplateController
 *@author 韩旭
 *@version 1.0.0
 *@create 2023/8/21 18:54
 **/
@RestController
@RequestMapping("template")
public class TemplateController {
    @Autowired
    private TemplateService templateService;

    @GetMapping("getlist")
    public ResponseResult getlist(TemplateLsitForm templateLsitForm){
        return templateService.getlist(templateLsitForm);
    }

    @DeleteMapping("delete/{id}")
    @Transactional
    public ResponseResult delete(@PathVariable("id")String id,@RequestParam("uuidname") String uuidname){
        ResponseResult responseResult = new ResponseResult();
        File file = new File("D:\\upload\\" + uuidname);
        boolean delete = file.delete();
        if(!delete){
            templateService.removeById(id);
            responseResult.fail(ResponseStatus.DOWN_EXIT);
            return responseResult;
        }
        templateService.removeById(id);
        responseResult.success(ResponseStatus.DELETE_SUCCESS);
        return responseResult;
    }

    @PostMapping("upload")
    public ResponseResult upload(MultipartFile file){
        return templateService.upload(file);
    }
    @GetMapping("down")
    public void down(HttpServletResponse response,String uuidname){
        templateService.down(response,uuidname);
    }

}
